package com.jl.obulibrary.driver;

public final class PasmHead
{
  //私有构造方法
  private PasmHead()
  {
  }

  //SAM卡工作时针,默认为4Mhz
  public static final byte ICC_CLOCK_12M = 0x0C;
  public static final byte ICC_CLOCK_6M = 0x06;
  public static final byte ICC_CLOCK_4M = 0x04;
  public static final byte ICC_CLOCK_3M = 0x03;
  public static final byte ICC_CLOCK_2M = 0x02;
  public static final byte ICC_CLOCK_1M = 0x01;

  //PSAM卡槽,现在能用的只有PSAM1,PSAM2
  public static final byte ICC_SOCK_PSAM1 = 0x00;
  public static final byte ICC_SOCK_PSAM2 = 0x01;
  public static final byte ICC_SOCK_PSAM3 = 0x02;
  public static final byte ICC_SOCK_PSAM4 = 0x03;
  public static final byte ICC_SOCK_PSAM5 = 0x04;
  public static final byte ICC_SOCK_PSAM6 = 0x05;

  //波特率
  public static final byte ICC_BAUD_9600  = 0x00;
  public static final byte ICC_BAUD_38400 = 0x01;
  public static final byte ICC_BAUD_115200 = 0x02;


  public static String addSpaceForResult(String data)
  {
    StringBuilder builder = new StringBuilder();
    char[] b = data.toCharArray();
    int  i = 0;
    for(char c : b)
    {
      if(i == 2)
      {
        i = 0;
        builder.append(" ");
      }
      builder.append(c);
      i ++;
    }
    return builder.toString();
  }

  private static int parse(char c)
  {
    if (c >= 'a')
      return (c - 'a' + 10) & 0x0f;
    if (c >= 'A')
      return (c - 'A' + 10) & 0x0f;
    return (c - '0') & 0x0f;
  }

  public static byte[] HexString2Bytes(String hexstr)
  {
    byte[] b = new byte[hexstr.length() / 2];
    int j = 0;
    for (int i = 0; i < b.length; i++)
    {
      char c0 = hexstr.charAt(j++);
      char c1 = hexstr.charAt(j++);
      b[i] = (byte) ((parse(c0) << 4) | parse(c1));
    }
    return b;
  }

  private final static byte[] hex = "0123456789ABCDEF".getBytes();
  public static String Bytes2HexString(byte[] b)
  {
    byte[] buff = new byte[2 * b.length];
    for (int i = 0; i < b.length; i++)
    {
      buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
      buff[2 * i + 1] = hex[b[i] & 0x0f];
    }
    return new String(buff);
  }

  /**
   *
   * @param hint * String
   * @param b * byte[]
   * @return void
   */
  public static void printBytesString(String hint, byte[] b)
  {
    System.out.print(hint);
    for(int i = 0; i < b.length; i++)
    {
      String hex = b[i]+"";
      if (hex.length() == 1)
      {
        hex = '0' + hex;
      }
      System.out.print(hex.toUpperCase() + " ");
    }
    System.out.println("");
  }

  public static void printHexString(String hint, byte[] b)
  {
    System.out.print(hint);
    for(int i = 0; i < b.length; i++)
    {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1)
      {
        hex = '0' + hex;
      }
      System.out.print(hex.toUpperCase() + " ");
    }
    System.out.println("");
  }
}